//
// HighlightDefinition.cs
//
// Author: 
//  Fredy Ramirez <framirez@pacificcoolwave.com>
//
// Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)
//
//  Creation date: Oct 12, 2011
//  Last modification: 

/*
The MIT License

Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */


using System;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using Pcw.Addins.Highlighting.Definition;

namespace Pcw.Addins.Highlighting
{
	public class HighlightDefinition
	{
		private string label;
		private bool case_sensitive;
		private string xml_file;
		private string[] extensions;
		private string root_dir = "";
		private Styles styles;
		private Descriptors descriptors;
		private Separators separators;
		
		private bool loaded = false;
		
		internal Descriptors Descriptors {
			get {
				return descriptors;
			}
		}
		
		public bool CaseSensitive {
			get {
				return case_sensitive;
			}
		}
		
		internal HighlightDefinition (string label, string xmlFile, string fileExtensions)
		{
			this.label = label;
			if (root_dir == "")
				root_dir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "syntax");
			xml_file = Path.Combine(root_dir, xmlFile);
			extensions = fileExtensions.Split(',');
		}

		internal bool IsExtension(string ext) 
		{
			if (ExtensionFound(ext)) {
				if (!loaded)
					SetDefinition();
				return true;
			}
			return false;
		}
		
		internal bool IsExtension(string ext, StringReader reader) 
		{
			if (ExtensionFound(ext)) {
				if (!loaded)
					SetDefinition(reader);	
				return true;
			}
			return false;
		}
				
		
		internal bool IsExtension(string ext, string syntaxFile)
		{
			if (ExtensionFound(ext)) {
				if (!loaded)
					SetDefinition(syntaxFile);
				return true;
			}
			return false;
		}
		
		private void SetDefinition () 
		{
			XmlDocument document = new XmlDocument();
			document.Load(xml_file); // Use file defined in definition file 'syntax/definition.xml'
			SetDefinition(document);
		}
		
		private void SetDefinition (StringReader reader) 
		{
			XmlDocument document = new XmlDocument();
			document.Load(reader); // Use an specific StringReader object.
			SetDefinition(document);
		}

		private void SetDefinition (string syntaxFile) 
		{
			XmlDocument document = new XmlDocument();
			document.Load(syntaxFile); // Use an specific syntax file.
			SetDefinition(document);
		}		
		
		private void SetDefinition (XmlDocument document) 
		{
			case_sensitive = XmlUtil.GetBoolean(document.DocumentElement, "caseSensitive");
							
			foreach (XmlNode node in document.DocumentElement.ChildNodes) 
			{
				switch(node.Name) 
				{
					case "styles":
						styles = new Styles(node);
						break;
					
					case "separators":
						separators = new Separators(node);
						break;
			
					case "descriptors":
						descriptors = new Descriptors(node, styles, separators, case_sensitive);
						break;
				}
			}
			
			loaded = true;
		}		
		
		private bool ExtensionFound(string ext) 
		{
			foreach(string s in extensions) 
				if (s.Trim() == ext) 
					return true;
			
			return false;
		}		
	}
}
